/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ekaaty.dao;

import br.com.ekaaty.util.HibernateUtil;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author otavio
 */
public abstract class CRUDGeneric<T> {

    protected  T bean;
    protected TypeOperation type;
    protected Session session;

    public T adicionar(T bean) {
        type = TypeOperation.CREATE;
        OpenSession();
        session.save(bean);
       
        CloseSession();
        return bean;
    }

    public void remover(T bean) {
        type = TypeOperation.DELETE;
        OpenSession();
        session.delete(bean);
       CloseSession();
    }

    public T atualizar(T bean) {
        type = TypeOperation.UPDATE;
        OpenSession();
        session.update(bean);
        CloseSession();

        return bean;
    }

    public abstract T recuperar(int cod);

    public abstract List<T> listar();

    public abstract boolean ehUnico(T bean);

    protected void OpenSession() {
        session = HibernateUtil.getInstance();
        session.getTransaction().begin();

    }

    protected void CloseSession() {
        session.getTransaction().commit();
        session.close();

    }

    /**
     * @return the bean
     */
    public T getBean() {
        return bean;
    }

    /**
     * @param bean the bean to set
     */
    public void setBean(T bean) {
        this.bean = bean;
    }
    
}
